<template>
    <div class="login-container">
      <div class="login-box">
        <div class="login-header">
          <h1>智慧农业大棚监控系统</h1>
        </div>
        <div class="login-form">
          <div class="form-item">
            <input 
              type="text" 
              v-model="username" 
              placeholder="请输入用户名"
            >
          </div>
          <div class="form-item">
            <input 
              type="password" 
              v-model="password" 
              placeholder="请输入密码"
            >
          </div>
          <div class="form-item">
            <button @click="handleLogin" :disabled="loading">
              {{ loading ? '登录中...' : '立即登录' }}
            </button>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref } from 'vue'
  import { useRouter } from 'vue-router'
  import { userApi } from '@/api'
  
  const router = useRouter()
  const username = ref('')
  const password = ref('')
  const loading = ref(false)
  
  const handleLogin = async () => {
    if (!username.value || !password.value) {
      alert('请输入用户名和密码')
      return
    }
  
    loading.value = true
    try {
      const response = await userApi.login({
        username: username.value,
        password: password.value
      })
  
      if (response && response.token) {
        localStorage.setItem('token', `Bearer ${response.token}`)
        localStorage.setItem('username', response.username)
        localStorage.setItem('role', response.role)
        localStorage.setItem('userId', response.id)
        localStorage.setItem('isLoggedIn', 'true')
        router.push('/dashboard')
      } else {
        alert('登录失败：用户名或密码错误')
      }
    } catch (error) {
      console.error('Login error:', error)
      alert('登录失败：' + (error.message || '网络错误'))
    } finally {
      loading.value = false
    }
  }
  </script>
  
  <style scoped>
  .login-container {
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
  }
  
  .login-box {
    width: 400px;
    padding: 40px;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    border-radius: 10px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  }
  
  .login-header {
    text-align: center;
    margin-bottom: 40px;
  }
  
  .login-header h1 {
    color: #fff;
    font-size: 24px;
    margin: 0;
  }
  
  .form-item {
    margin-bottom: 20px;
  }
  
  .form-item input {
    width: 100%;
    height: 45px;
    padding: 0 15px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    font-size: 16px;
    box-sizing: border-box;
  }
  
  .form-item input::placeholder {
    color: rgba(255, 255, 255, 0.5);
  }
  
  .form-item button {
    width: 100%;
    height: 45px;
    border: none;
    border-radius: 5px;
    background: #42b983;
    color: #fff;
    font-size: 16px;
    cursor: pointer;
    transition: background 0.3s;
  }
  
  .form-item button:hover {
    background: #3aa876;
  }
  </style> 